
//app.js
import Storage from './utils/storage'
App({
	globalData: {},
	onLaunch: async function () {
		this.setNavBarInfo()
		// Storage.clear()
		try {
			// 等待存储初始化完成
			await this.initializeApp()
		} catch (error) {
			console.error('App初始化失败:', error)
		}
	},
	// 初始化应用
	initializeApp: function() {
		return new Promise((resolve) => {
			// 确保存储系统就绪
			const token = Storage.get('token') || null
			if (!token) {
				this.navigateToLogin()
			}
		})
	},
	// onLaunch: function () {
	// 	// wx.clearStorage()
	// 	Storage.clear()
	// 	const token = Storage.get('token') || null
	// 	if (!token) {
	// 		wx.navigateTo({
	// 			url: '/pages/login/index'
	// 		})
	// 	}
	// 	this.setNavBarInfo()
	// },
	navigateToLogin: function () {
		// wx.redirectTo({
		// 	url: '/pages/login/index', // 确保路径正确
		// 	success: (res) => {
		// 		console.log('跳转成功:', res)
		// 	},
		// 	fail: (error) => {
		// 		console.error('跳转失败:', error)
		// 	},
		// 	complete: (res) => {
		// 		console.log('跳转完成:', res)
		// 	}
		// })
	},

	/**
	* @description 设置导航栏信息
	*/
	setNavBarInfo() {
		// 获取系统信息
		const systemInfo = wx.getSystemInfoSync()
		// 胶囊按钮位置信息
		const menuButtonInfo = wx.getMenuButtonBoundingClientRect()
		const navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight
		// 导航栏高度 = 状态栏到胶囊的间距（胶囊距上距离-状态栏高度） * 2 + 胶囊高度 + 状态栏高度
		this.globalData.navBarHeight = (menuButtonInfo.top - systemInfo.statusBarHeight) * 2 + menuButtonInfo.height + systemInfo.statusBarHeight
		this.globalData.menuBotton = menuButtonInfo.top - systemInfo.statusBarHeight
		this.globalData.menuRight = systemInfo.screenWidth - menuButtonInfo.right
		this.globalData.menuHeight = menuButtonInfo.height
		this.globalData.doalogHeight = systemInfo.windowHeight - navBarHeight
	}
})
